import Vue from "vue";
import router from "@/router/index";
// progress bar -- start
import NProgress from "nprogress";
import "nprogress/nprogress.css";
// progress bar -- end
import config from "@/router/router.config";

NProgress.configure({showSpinner: false}); // NProgress Configuration

console.log("****加载权限,这里写校验token的逻辑****");

router.beforeEach((to, from, next) => {
    NProgress.start();
    //设置title
    if (to.name && to.name != "" && to.name != "undefined") {
        document.title = window.global_value.VUE_APP_DEFAULT_TITLE + "-" + to.name;
    } else {
        document.title = window.global_value.VUE_APP_DEFAULT_TITLE;
    }
    console.log("to.path", to.path);
    //[/test001]测试登录和菜单
    if (to.path.startsWith("/test001")) {
        //检查token
        let token = Vue.ls.get(window.global_value.TOKEN_KEY);
        if (token && token != '') {
            /* has token */
            console.log("****有token，放行****");
            if (to.path == "/test001/login") {
                //登录成功之后，不能访问login
                next({path: "/test001/homeIndex"});
            } else {
                next();
            }
        } else {
            /* has not token */
            if (config.whiteList.indexOf(to.path) !== -1) {
                //在白名单请求中，放行
                console.log("****没有token，在白名单，放行****");
                next();
            } else {
                next({path: "/test001/login", query: {redirect: to.fullPath}});
            }
        }
    }else{
        //其他不校验
        next();
    }
});

router.afterEach(() => {
    NProgress.done(); // finish progress bar
});
